<template>
  <div class="directorys-container" style="padding: 10px">
    <el-card>
      <!-- 头部 -->
      <el-breadcrumb v-show="id" separator-class="el-icon-arrow-right" style="border-bottom: 1px solid #ebeef5;padding-bottom: 18px;margin-bottom:20px">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{name}}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-row>
        <el-col :span="18">
          <el-form :inline="true" ref="query" :model="query" class="demo-form-inline">
            <el-form-item label="目录名称">
              <el-input v-model="query.directoryName"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="query.state" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="mini" @click="onClear">清除</el-button>
              <el-button type="primary" @click="onSubmit" size="mini"
                >搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <div class="display__flex">
            <el-link v-show="id ? true : false" type="primary" style="margin-right:10px" icon='el-icon-back' @click="$router.back()">返回学科</el-link>
              <el-button
                type="success"
                size="mini"
                icon="el-icon-edit"
                @click="directorysTrue=true"
                >新增目录</el-button
              >
          </div>
          <directorys-add  :row='row' v-model="directorysTrue" @close='onClose' @result='getList' />
        </el-col>
      </el-row>
      <!-- 提示多少条 -->
      <el-alert
        :title="'数据一共 ' + counts + ' 条'"
        type="info"
        show-icon
        :closable="false"
      />
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%; padding: 10px"
        v-loading="loading"
        element-loading-text="给我一点时间"
         :header-cell-style="{background:'#FAFAFA'}"
        fit
        highlight-current-row>

        <el-table-column type="index" label="序号" />
        <el-table-column prop="subjectName" label="所属学科" />
        <el-table-column prop="directoryName" label="目录名称" />
        <el-table-column prop="username" label="创建者" />
        <el-table-column label="创建日期">
          <template slot-scope="{ row }">
            {{ row.addDate | date }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="{ row }">
            {{ row.state === 1 ? "启用" : "禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{row}">
            <el-button type="text" size="mini" @click="onChange(row)">{{ row.state === 1 ? "禁用" : "启用" }}</el-button>
            <el-button type="text" size="mini" :disabled="row.state === 1 ? true :false" @click="editDirectorys(row)">修改</el-button>
            <el-button type="text" size="mini" :disabled="row.state === 1 ? true :false" @click="removeDirectorys(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      hide-on-single-page
      style="text-align:right;margin-top:5px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :background='true'
      :current-page="query.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="query.pagesize"
      layout=" sizes, prev, pager, next, jumper"
      :total="counts"/>
    </el-card>
  </div>
</template>

<script>
import directorysAdd from '../components/directorys-add.vue'
import { list, changeState, remove } from '@/api/hmmm/directorys'
import dayjs from 'dayjs'
// import baseApi from '@/api/base/baseApi.'
export default {
  name: 'directorys',
  components: { directorysAdd },
  data () {
    return {
      query: {
        // 页数
        pagesize: 10,
        // 目录名称
        directoryName: '',
        // 状态
        state: '',
        // 当前页码
        page: 1,
        // 学科id
        subjectID: ''
      },
      tableData: [],
      counts: 0,
      directorysTrue: false,
      row: {},
      loading: true,
      id: this.$route.query.id, // 路由传过来的id
      name: this.$route.query.name // 路由传过来的name
    }
  },
  filters: {
    date (val) {
      return dayjs(dayjs(val).$d).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  created () {
    this.query = {}
    this.getList()
  },
  methods: {
    // 发起搜索请求
    onSubmit () {
      this.getList()
      // console.log(res)
    },
    // 获取目录列表
    async getList () {
      if (this.id) {
        const res = await list({ subjectID: this.id })
        const { items, counts } = res.data
        this.tableData = items
        this.counts = counts
      } else {
        const res = await list(this.query)
        const { items, counts } = res.data
        this.tableData = items
        this.counts = counts
      }
      // console.log(res)
      // const { items, counts } = res.data
      // this.tableData = items
      // this.counts = counts
      this.loading = false
    },
    // 清除表单数据
    onClear () {
      this.query = {}
      this.getList()
    },
    // 修改状态
    async onChange (row) {
      const { state } = row
      if (state === 1) {
        await changeState({ state: row.state - 1, id: row.id })
      }
      if (state === 0) {
        await changeState({ state: row.state + 1, id: row.id })
      }
      this.$message.success('修改成功')
      this.getList()
    },
    // 删除目录
    removeDirectorys (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: row.id })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 修改目录
    editDirectorys (row) {
      this.directorysTrue = true
      this.row = row
    },
    // 把父组件的传过去的值清空
    onClose () {
      this.row = {}
    },
    handleSizeChange (val) {
      console.log(val)
      this.query.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      if (!this.query.directoryName) {
        this.query.page = val
        this.getList()
      } else {
        this.getList()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.display__flex{
  display: flex;
  justify-content: flex-end;
}
</style>
